package com.letv.so.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * 静态方法类：为 详情页/站点源 等接口根据不同from参数的特殊需求返回不同的特殊参数
 */
public class SpecialFieldUtil {
    public static final String WEBSITE_NOT_CONTIANT = "website_vl";
    public static final String FILEDS_SEP_EQU = "=";
    public static final String WEBSITE_REMOVE = "website_remove";
    public static final String WEBSITE_REMOVE_RETAIN = "retain";
    public static final String WEBSITE_REMOVE_YES = "remove";
    /**
     * from端在访问srcName页面时是否需要 增加/移除 field字段
     * 例如：SARRS(mobile_11)在访问详情页(leso)时是否需要 增加/移除 name字段
     * @param rtnkeys Map<String, Map<String,List<String>>> 配置文件中需要特殊处理的字段
     * @param from String 访问者
     * @param srcName String 访问的页面标志，例如:leso,website...
     * @param field String 字段
     * @param addOrRemove true--判断是否需要增加      false--判断是否需要移除
     * @return boolean 需要--true 不需要--false
     */
    public static boolean checkSingleField(Map<String, Map<String,List<String>>> rtnkeys, String from, String srcName, String field, boolean addOrRemove) {
        if(null == rtnkeys || rtnkeys.size() == 0) {
            return false;
        }
        if(StringUtils.isBlank(srcName) || StringUtils.isBlank(field) || StringUtils.isBlank(from)) {
            return false;
        }
        boolean back = false;
        try {
            Map<String,List<String>> config = null;
            if(addOrRemove) {
                config = rtnkeys.get(srcName + "_add");
            } else {
                config = rtnkeys.get(srcName + "_remove");
            }
            if (null != config) {
                List<String> keys = config.get(from);
                if(null == keys || keys.size() == 0) {
                    return false;
                }
                for(String key : keys) {
                    if(!StringUtils.isBlank(key) && field.equals(key)) {
                        back = true;
                        break;
                    }
                }
            }
        } catch(Exception e) {
            back = false;
        }
        return back;
    }
    
    public static boolean canContain(Map<String, Map<String,List<String>>> rtnkeys, String from,String srcName,String fieldEqValue,boolean defaultB){
        if(null == rtnkeys || rtnkeys.size() == 0) {
            return defaultB;
        }
        if(StringUtils.isBlank(fieldEqValue) || StringUtils.isBlank(from)) {
            return defaultB;
        }
        boolean back = defaultB;
        Map<String,List<String>> config = rtnkeys.get(srcName);
        if (config != null) {
            List<String> list = config.get(from);
            if (list !=null  && list.contains(fieldEqValue)) {
                back = !defaultB;
            }
        }
        return back;
    }

    public static  List<String> getConfigList(
            Map<String, Map<String, List<String>>> rtnkeys, String from,
            String srcName) {
        if(null == rtnkeys || rtnkeys.size() == 0) {
            return null;
        }
        Map<String,List<String>> config = rtnkeys.get(srcName);
        if (config != null) {
            return config.get(from);
        }
        return null;
    }

    public static boolean matchConfig(List<String> fileds, Object obj,String sep) {
        int size = fileds.size();
        boolean[] rtns = new boolean[size];
        for (int i=0;i<size;i++) {
            String[] kv = fileds.get(i).split(sep);
            boolean isMatch =false;
            if (kv.length == 2) {
                isMatch= kv[1].equals(ReflectUtil.invokeGetMethod(obj, kv[0]));
            }
            rtns[i] = isMatch;
        }
        return size==0?false:getResultBoolean(rtns);
    }

    private static boolean getResultBoolean(boolean[] rtns) {
        boolean rt = true;
        for (boolean a:rtns) {
            rt &=a;
        }
        return rt;
    }

}
